package com.ojoin.trade.t0loan_core.web.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.controller.BaseControllerImpl;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.Result.Status;
import com.ojoin.trade.common.web.service.BaseService;
import com.ojoin.trade.common_core.web.domain.TbMerchantUser;
import com.ojoin.trade.common_core.web.service.MerchantUserBaseService;
import com.ojoin.trade.common_core.web.service.impl.DataVersionValidationService;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanCheckAccountInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanTransanctionLogInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanFinalApplication;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanLiveApplication;
import com.ojoin.trade.t0loan_core.web.service.IntentUserBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanFinalBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanLiveBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanTransanctionLogBaseService;

@Controller
@RequestMapping("/t1loan")
public class T1LoanLiveApplicationController extends BaseControllerImpl<TbT0LoanLiveApplication>{
	
	private Logger log=LoggerFactory.getLogger(T1LoanLiveApplicationController.class);
	
	@Autowired
	private DataVersionValidationService validateService;
	
	public DataVersionValidationService getValidateService() {
		return validateService;
	}
	
	@Autowired
	private T0LoanLiveBaseService liveservice;

	public T0LoanLiveBaseService getLiveservice() {
		return liveservice;
	}

	@Autowired
	private IntentUserBaseService intentuserService;

	public IntentUserBaseService getIntentuserService() {
		return intentuserService;
	}

	
	@Autowired
	private T0LoanFinalBaseService finalservice;

	public T0LoanFinalBaseService getFinalservice() {
		return finalservice;
	}
	
	@Autowired
	private T0LoanTransanctionLogBaseService transservice;

	public T0LoanTransanctionLogBaseService getTransservice() {
		return transservice;
	}
	
	@Autowired
	private IntentUserBaseService intentUserService;

	public IntentUserBaseService getIntentUserService() {
		return intentUserService;
	}
	
	@Autowired
	private MerchantUserBaseService userService;
	
	@ResponseBody
	@RequestMapping(value = "/inserttxliveinfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result inserttxliveinfo(HttpServletRequest request, @RequestParam String operatetype) {
		try {
              int joinuserid=CommonUtils.getSessionUser(request).getId();

              TbT0LoanLiveApplication livevo = getLiveservice().queryNewlyTxLoanLiveInfoByJoinUserID(joinuserid, operatetype);
              if (livevo != null && livevo.getLoanstatus() != IConstDef.TX_LOANLIVETYPE_REFUSE) {
            	  return new Result(Status.WARNING, "您已经提交过申请，请刷新页面.");
              } else {
					int liveid = getLiveservice().insertTxLoanLiveInfo(joinuserid, operatetype);
		            return new Result(Status.OK, liveid);
              }
		} catch (Exception e) {
			log.error("九盈用户id新增" + operatetype +"贷款申请记录报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "九盈用户id新增" + operatetype +"贷款申请记录报错，请重试");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/queryTxliveinfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryTxliveinfo(HttpServletRequest request, @RequestParam String operatetype) {
		int joinuserid=CommonUtils.getSessionUser(request).getId();
		try {
			TbT0LoanLiveApplication livevo = getLiveservice().queryNewlyTxLoanLiveInfoByJoinUserID(joinuserid, operatetype);
             return new Result(Status.OK, livevo);
		} catch (Exception e) {
			log.error("根据九盈用户id查询用户的最近一次" + operatetype +"贷款申请记录报错", e);
			return new Result(Status.ERROR, "根据九盈用户id查询用户的最近一次" + operatetype +"贷款申请记录报错，请重试");
		}
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = "/queryTxfinalinfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryTxfinalinfo(HttpServletRequest request) {
		int joinuserid=CommonUtils.getSessionUser(request).getId();
		try {
			TbT0LoanFinalApplication finalvo = getFinalservice().queryTxLoanFinalInfoByJoinUserID(joinuserid);
             return new Result(Status.OK, finalvo);
		} catch (Exception e) {
			log.error("根据九盈用户id查询用户的垫款申请记录报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "根据九盈用户id查询用户的垫款申请记录报错，请重试");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/queryTxLoanBalance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryTxLoanBalance(HttpServletRequest request) {
		int joinuserid=CommonUtils.getSessionUser(request).getId();
		try {
			TbMerchantUser uservo = userService.queryById(joinuserid);
            return new Result(Status.OK, uservo);
		} catch (Exception e) {
			log.error("根据九盈用户id查询用户的垫款业务实时负债信息报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "根据九盈用户id查询用户的垫款业务负债信息报错，请重试");
		}
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/queryTxLoanBalanceInfoByDebtorMhtno", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryTxLoanBalanceInfoByDebtorMhtno(HttpServletRequest request) {
		String debtor_mhtno = CommonUtils.getSessionUser(request).getMhtNo();
		try {
			T0LoanCheckAccountInfoVO vo = getTransservice().queryTxLoanBalanceInfoByDebtorMhtno(debtor_mhtno);
            return new Result(Status.OK, vo);
		} catch (Exception e) {
			log.error("根据商户号查询用户的负债信息报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "根据商户号查询用户的负债信息报错，请重试");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/queryTxLoanTransanctionLogInfoListByDebtorMhtNo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryTxLoanTransanctionLogInfoListByDebtorMhtNo(HttpServletRequest request, Pageable page) {
		String debtor_mhtno = CommonUtils.getSessionUser(request).getMhtNo();
		int joinuserid=CommonUtils.getSessionUser(request).getId();
		try {
			TbT0LoanFinalApplication finalvo = finalservice.queryTxLoanFinalInfoByJoinUserID(joinuserid);
			List<T0LoanTransanctionLogInfoVO> transloglist = getTransservice().queryTxLoanTransanctionLogInfoListByDebtorMhtNo(debtor_mhtno, finalvo.getOperationType(), page);
            return new Result(Status.OK, transloglist);
		} catch (Exception e) {
			log.error("根据商户号查询用户的负债信息列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "根据商户号查询用户的负债信息列表报错，请重试");
		}
	}
	
	@Override
	protected BaseService<TbT0LoanLiveApplication> getBaseService() {
		return null;
	}
}
